let DOAIM_PREFIX = 'http://localhost:8888/wechat/h5/';
let API_CUSTOM_URL = DOAIM_PREFIX + 'custom.php';
let orginopenid = getQueryString('orginopenid');
let xhrCustom = $.post(API_CUSTOM_URL, {orginopenid: orginopenid});
let firstlight = null;
let flNum = 0;
let mpqrcode_url = '';
let mpqrcode = $('#mpqrcode');
let yanshengImgIdPrefix = '#yanshenNo';

let yanshen4 = '.yanshen4';


xhrCustom.done(function (res) {
  if (res.code !== 0) {
    return
  }
  firstlight = res.data.firstlight;
  flNum = res.data.count;
  mpqrcode_url = res.data.url;
  customPage()
});

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

function customPage() {
  let count = flNum;
  mpqrcode.qrcode({
    render: 'table',
    width: mpqrcode.width(),
    height: mpqrcode.height(),
    text: mpqrcode_url
  });

  let oSrc = $(yanshengImgIdPrefix + (firstlight + 1)).attr('src');

  let nSrc = oSrc.replace('0', '1');

  $(yanshengImgIdPrefix + (firstlight + 1)).attr('src', nSrc);
  $(yanshengImgIdPrefix + (firstlight + 1)).attr('data-light', 1);

  $(yanshen4).find('.yanshen').each(function (i, e) {

    if (count === 0) {

      return
    }
    if ($(e).attr('data-light') === '0') {

      $(e).attr('data-light', 1);
      $(e).attr('src', changeFlYanshen($(e)));
      count--
    }

  });

  function changeFlYanshen($e) {
    let oSrc = $e.attr('src');
    let nSrc = oSrc.replace('0', '1');

    return nSrc
  }

}
